<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>运维巡查维护界面</title>
    <!-- <script src="https://cdn.staticfile.org/vue/2.2.2/vue.min.js"></script> -->
    <script src="https://cdn.staticfile.org/axios/0.18.0/axios.min.js"></script>
    <!-- 引入样式 -->
    <!-- <link rel="stylesheet" href="./css/index.css" /> -->
    <!-- 引入组件库 -->
    <!-- <script src="./js/element-ui-index.js"></script> -->
    <!-- 引入样式 -->
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">

    <!-- import Vue before Element -->
    <script src="https://unpkg.com/vue@2/dist/vue.js"></script>
    <!-- import JavaScript -->
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
</head>

<body>
    <div id="app" style="width: 70%;margin: auto;">
        <el-tabs v-model="activeName" @tab-click="handleClick">
            <el-tab-pane label="每日信息填报" name="dailyForm">
                <el-row :gutter="15">
                    <el-form ref="myform" :model="myform" :rules="rules" size="medium" label-width="100px"
                        label-position="top">
                        <el-col :span="12">
                            <el-form-item label="温度情况" prop="Patrol.temperature_status">
                                <el-radio-group v-model="myform.Patrol.temperature_status" size="medium">
                                    <el-radio v-for="(item, index) in temperature_statusOptions" :key="index"
                                        :label="item.value" :disabled="item.disabled" border>{{item.label}}</el-radio>
                                </el-radio-group>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="供电情况" prop="Patrol.electric_status">
                                <el-radio-group v-model="myform.Patrol.electric_status" size="medium">
                                    <el-radio v-for="(item, index) in electric_statusOptions" :key="index"
                                        :label="item.value" :disabled="item.disabled" border>{{item.label}}</el-radio>
                                </el-radio-group>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label="机房环境" prop="Patrol.environment_status">
                                <el-radio-group v-model="myform.Patrol.environment_status" size="medium">
                                    <el-radio v-for="(item, index) in environment_statusOptions" :key="index"
                                        :label="item.value" :disabled="item.disabled" border>{{item.label}}</el-radio>
                                </el-radio-group>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label-width="200px" label="BES实例情况" prop="BES_status">
                                <el-radio-group v-model="myform.Server.BES_status" size="medium">
                                    <el-radio v-for="(item, index) in BES_statusOptions" :key="index"
                                        :label="item.value" :disabled="item.disabled" border>{{item.label}}</el-radio>
                                </el-radio-group>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label-width="200px" label="服务器状态" prop="server_status">
                                <el-radio-group v-model="myform.Server.server_status" size="medium">
                                    <el-radio v-for="(item, index) in server_statusOptions" :key="index"
                                        :label="item.value" :disabled="item.disabled" border>{{item.label}}</el-radio>
                                </el-radio-group>
                            </el-form-item>
                        </el-col>
                        <el-col :span="12">
                            <el-form-item label-width="200px" label="备份状态" prop="backup_status">
                                <el-radio-group v-model="myform.Server.backup_status" size="medium">
                                    <el-radio v-for="(item, index) in backup_statusOptions" :key="index"
                                        :label="item.value" :disabled="item.disabled" border>{{item.label}}</el-radio>
                                </el-radio-group>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label-width="200px" label="应用状态" prop="app_status">
                                <el-radio-group v-model="myform.Server.app_status" size="medium">
                                    <el-radio v-for="(item, index) in app_statusOptions" :key="index"
                                        :label="item.value" :disabled="item.disabled" border>{{item.label}}</el-radio>
                                </el-radio-group>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="备注" prop="remark">
                                <el-input v-model="myform.remark" type="textarea" placeholder="如有特殊情况，请说明！"
                                    :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item label="key" prop="key">
                                <el-input v-model="myform.key" placeholder="请输入key以进行提交" clearable
                                    :style="{width: '100%'}">
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item size="large">
                                <el-button type="primary" @click="submitForm">提交</el-button>
                                <el-button @click="resetForm">重置</el-button>
                            </el-form-item>
                        </el-col>
                    </el-form>
                </el-row>
            </el-tab-pane>
            <el-tab-pane label="信息统计" name="count">
                <el-table :data="countData" style="width: 100%"
                    :default-sort="{prop: 'createTime', order: 'descending'}">
                    <el-table-column prop="createTime" label="填写时间" sortable width="180">
                    </el-table-column>
                    <el-table-column prop="patrol" label="机房巡查项" sortable width="180">
                    </el-table-column>
                    <el-table-column prop="server" label="服务器检查项" sortable width="180">
                    </el-table-column>
                    <el-table-column prop="ip" label="填写来源地址" sortable width="180">
                    </el-table-column>
                    <el-table-column label="操作" width="180">
                        <template slot-scope="scope">
                            <el-button @click="getCountData(scope.row.id)" type="text" size="small">查看详情</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <el-pagination background layout="prev, pager, next" :total="page.total" :index="page.index"
                    @current-change="handleCurrentChange" size="page.size">
                </el-pagination>


            </el-tab-pane>
            <el-tab-pane label="ssh信息录入" name="sshInsert">
                <el-row :gutter="15">
                    <el-form ref="sshValue" :model="sshFormData" :rules="rules1" size="medium" label-width="100px">
                        <el-col :span="6">
                            <el-form-item label="ssh来源" prop="username">
                                <el-input v-model="sshFormData.username" placeholder="请输入ssh来源" clearable
                                    :style="{width: '100%'}">
                                </el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="20">
                            <el-form-item label="ssh公钥" prop="sshValue">
                                <el-input v-model="sshFormData.sshValue" type="textarea" placeholder="请输入ssh公钥"
                                    :autosize="{minRows: 4, maxRows: 4}" :style="{width: '100%'}"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="24">
                            <el-form-item size="large">
                                <el-button type="primary" @click="submitSshForm">提交</el-button>
                                <el-button @click="resetSshForm">重置</el-button>
                            </el-form-item>
                        </el-col>
                    </el-form>
                </el-row>
            </el-tab-pane>
            <el-tab-pane label="已录入的ssh信息" name="sshDetail" ref="rushSshDetail">
                <el-table :data="sshData" style="width: 100%" :default-sort="{prop: 'createTime', order: 'descending'}">
                    <el-table-column prop="createTime" label="填写时间" sortable width="180">
                    </el-table-column>
                    <el-table-column prop="username" label="来源" sortable width="180">
                    </el-table-column>
                    <el-table-column label="操作" width="180">
                        <template slot-scope="scope">
                            <el-button @click="viewSshData(scope.row.id)" type="text" size="small">查看</el-button>
                            <el-button @click="deleteSshData(scope.row.id,scope.row.username)" type="text"
                                size="small">删除信息</el-button>
                        </template>
                    </el-table-column>
                </el-table>
                <el-pagination background layout="prev, pager, next" :total="sshPage.total" :index="sshPage.index"
                    @current-change="sshPageChange" size="sshPage.size">
                </el-pagination>
            </el-tab-pane>
            <el-dialog title="每日填报详情" :visible.sync="dialogVisible" width="50%">
                编写时间 ：{{resultData.createTime}}
                <div v-for="(v,k) in resultData.Patrol">
                    <span>{{k}} -- {{v==="ok"?'正常':'异常'}}</span>
                </div><br>
                <div v-for="(v1,k1) in resultData.Server">
                    <span>{{k1}} -- {{v1==="ok"?'正常':'异常'}}</span>
                </div><br>
                备注：--- {{resultData.remark}}
                <br><br>
                来源地址：{{resultData.ip == null ? '过时数据，暂无来源！' : resultData.ip}}
            </el-dialog>
            <el-dialog title="ssh信息" :visible.sync="sshDialog" width="50%">
                来源人 ： {{sshDetailData.username}}<br>
                创建时间：{{sshDetailData.createTime}}<br>
                ssh公钥的值：<br>
                {{sshDetailData.sshValue}}
            </el-dialog>
    </div>
</body>
<script>
    new Vue({
        el: '#app',
        data: {
            sshDetailData: {},
            sshDialog: false,
            sshData: {
                id: "",
                username: "",
                value: ""
            },
            sshFormData: {
                username: "",
                value: "",
            },
            rules1: {
                username: [{
                    required: true,
                    message: '请输入ssh来源',
                    trigger: 'blur'
                }],
                sshValue: [{
                    required: true,
                    message: '请输入ssh公钥',
                    trigger: 'blur'
                }],
            },
            resultData: {
                Patrol: {
                    temperature_status: "",
                    electric_status: "",
                    environment_status: ""
                },
                Server: {
                    BES_status: "",
                    server_status: "",
                    backup_status: "",
                    app_status: ""
                },
                createTime: "",
                remark: "",
                ip: null
            },
            dialogVisible: false,
            page: {
                index: 1,
                size: 10,
                total: 0
            },
            sshPage: {
                index: 1,
                size: 10,
                total: 0
            },
            activeName: 'dailyForm',
            myform: {
                Patrol: {
                    temperature_status: "no",
                    electric_status: "no",
                    environment_status: "no"
                },
                Server: {
                    BES_status: "no",
                    server_status: "no",
                    backup_status: "no",
                    app_status: "no"
                },
                remark: "",
                key: ""
            },
            temperature_statusOptions: [{
                "label": "正常",
                "value": "ok"
            }, {
                "label": "异常",
                "value": "no"
            }],
            electric_statusOptions: [{
                "label": "正常",
                "value": "ok"
            }, {
                "label": "异常",
                "value": "no"
            }],
            environment_statusOptions: [{
                "label": "正常",
                "value": "ok"
            }, {
                "label": "异常",
                "value": "no"
            }],
            BES_statusOptions: [{
                "label": "正常",
                "value": "ok"
            }, {
                "label": "异常",
                "value": "no"
            }],
            server_statusOptions: [{
                "label": "正常",
                "value": "ok"
            }, {
                "label": "异常",
                "value": "no"
            }],
            backup_statusOptions: [{
                "label": "正常",
                "value": "ok"
            }, {
                "label": "异常",
                "value": "no"
            }],
            app_statusOptions: [{
                "label": "正常",
                "value": "ok"
            }, {
                "label": "异常",
                "value": "no"
            }],
            rules: {
                key: [
                    { required: true, message: '必须填写key！', trigger: 'blur' }
                ]
            },
            countData: [],
            url: "http://127.0.0.1:8089"
            // url:"http://192.168.2.64:8089"
        },
        methods: {
            submitSshForm() {
                this.$refs['sshValue'].validate(valid => {
                    if (!valid) return
                    // TODO 提交表单
                    let url = this.url + "/demo/sshKey/"
                    let obj = this.sshFormData
                    axios({
                        method: "post",
                        url: url,
                        data: obj,
                        headers: {
                            'Content-Type': 'application/json;charset=UTF-8'
                        }
                    }).then(resp => {
                        if (resp.data.msg === "添加成功") {
                            this.$message({
                                message: resp.data.msg,
                                type: 'success'
                            });
                        } else {
                            this.$message.error(resp.data.msg);
                        }
                    })
                })
            },
            resetSshForm() {
                this.$refs['sshValue'].resetFields()
            },
            handleCurrentChange(val) {
                console.log(val)
                // 改变默认的页数
                this.page.index = val
                let tab = {
                    "name": "count"
                }
                this.handleClick(tab)
            },
            sshPageChange(val) {
                console.log(val)
                // 改变默认的页数
                this.sshPage.index = val
                let tab = {
                    "name": "sshDetail"
                }
                this.handleClick(tab)
            },
            handleClick(tab) {
                console.log(tab.name);
                if (tab.name === "count") {
                    console.log(1)
                    // 获取后端统计数据
                    let url = this.url + "/demo/dailyForm/?currentPage=" + this.page.index + "&size=" + this.page.size;
                    axios
                        .get(url)
                        .then(response => {
                            this.page.total = response.data.total
                            this.countData = response.data.list
                            console.log(this.countData)

                        })
                        .catch(function (error) { // 请求失败处理
                            console.log(error);
                        });

                }

                if (tab.name === "sshDetail") {
                    console.log(1)
                    // 获取后端统计数据
                    let url = this.url + "/demo/sshKey/?currentPage=" + this.sshPage.index + "&size=" + this.sshPage.size;
                    axios
                        .get(url)
                        .then(response => {
                            this.sshPage.total = response.data.total
                            this.sshData = response.data.list
                            console.log(this.sshData)

                        })
                        .catch(function (error) { // 请求失败处理
                            console.log(error);
                        });

                }


            },
            resetForm() {
                this.myform = {
                    Patrol: {
                        temperature_status: "",
                        electric_status: "",
                        environment_status: ""
                    },
                    Server: {
                        BES_status: "",
                        server_status: "",
                        backup_status: "",
                        app_status: ""
                    },
                    remark: "",
                    key: "",
                    ip: ""
                }
            },
            submitForm: function () {
                this.$refs['myform'].validate(valid => {
                    if (!valid) return
                    let url = this.url + "/demo/dailyForm/"
                    let obj = {
                        value: JSON.stringify(this.myform),
                        key: this.myform.key
                    }
                    // console.log(obj)
                    axios({
                        method: "post",
                        url: url,
                        data: obj,
                        headers: {
                            'Content-Type': 'application/json;charset=UTF-8'
                        }
                    }).then(resp => {
                        if (resp.data.msg === "添加成功") {
                            this.$message({
                                message: resp.data.msg,
                                type: 'success'
                            });
                        } else {
                            this.$message.error(resp.data.msg);
                        }
                    })
                })
            },
            getCountData(id) {
                // 访问后端 获取数据详情
                console.log(id)
                let url = this.url + "/demo/dailyForm/getById/" + id;
                axios
                    .get(url)
                    .then(response => {

                        let myJson = JSON.parse(response.data.data.value)
                        this.resultData.Patrol = myJson.Patrol
                        this.resultData.Server = myJson.Server
                        this.resultData.createTime = response.data.data.createTimeStr
                        this.resultData.remark = myJson.remark
                        this.resultData.ip = response.data.data.writerFrom
                        console.log(this.resultData)
                        this.dialogVisible = true
                    })
                    .catch(function (error) { // 请求失败处理
                        console.log(error);
                    });

            },
            deleteSshData(id, username) {
                console.log("id == ", id)
                this.$confirm('此操作将永久删除 ' + username + ' 的ssh信息, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    let url = this.url + "/demo/sshKey/" + id;
                    axios
                        .delete(url)
                        .then(response => {
                            if (response.data.msg === "删除成功") {
                                this.$message({
                                    message: response.data.msg,
                                    type: 'success'
                                });
                                // 重置页面
                                this.handleClick({ "name": "sshDetail" })
                            } else {
                                this.$message.error(response.data.msg);
                            }

                        })
                        .catch(function (error) { // 请求失败处理
                            console.log(error);
                        });
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消删除'
                    });
                });

            },
            viewSshData(id) {
                let url = this.url + "/demo/sshKey/" + id;
                axios
                    .get(url)
                    .then(response => {
                        console.log(response.data)
                        this.sshDetailData = response.data.data

                        this.sshDialog = true
                    })
                    .catch(function (error) { // 请求失败处理
                        console.log(error);
                    });
            }

        }
    })


</script>

</html>